import youtubeAPI
import unittest
import gdata.youtube

class TestYouTubeAPI(unittest.TestCase):
    
    def test_main(self):
        print "Testing YouTube search..."
        ytd = youtubeAPI.youtubeData()
        ytd.initializeAPI()
        self.SearchAndPrint("daft punk")
        self.assertTrue(ytd.getTopVideo("justice").media.title.text, "Justice - Civilization")
    
    def test_fail(self):
        ytd = youtubeAPI.youtubeData()
        ytd.initializeAPI()
        self.assertTrue(ytd.getTopVideo("39u8r9jzidfhjzkdf-3%@#%") == None)
        
    def PrintEntryDetails(self, entry):
        print 'Video title: %s' % entry.media.title.text
        print 'Video published on: %s ' % entry.published.text
        print 'Video description: %s' % entry.media.description.text
        print 'Video category: %s' % entry.media.category[0].text
        print 'Video tags: %s' % entry.media.keywords.text
        print 'Video watch page: %s' % entry.media.player.url
        print 'Video flash player URL: %s' % entry.GetSwfUrl()
        print 'Video duration: %s' % entry.media.duration.seconds
        
        # non entry.media attributes
        #print 'Video geo location: %s' % entry.geo.location()
        print 'Video view count: %s' % entry.statistics.view_count
        print 'Video rating: %s' % entry.rating.average
        
        # show alternate formats
        if (entry.media.content):
            for alternate_format in entry.media.content:
                if 'isDefault' not in alternate_format.extension_attributes:
                    print 'Alternate format: %s | url: %s ' % (alternate_format.type,
                                                           alternate_format.url)
        
        # show thumbnails
        for thumbnail in entry.media.thumbnail:
            print 'Thumbnail url: %s' % thumbnail.url
            
        print "\n \n" 
        
    def PrintVideoFeed(self,feed):
        entrylist = []
        for entry in feed.entry:
            entrylist.append(entry)
            self.PrintEntryDetails(entry)
        return entrylist
    
    def SearchAndPrint(self,search_terms):
        yt_service = gdata.youtube.service.YouTubeService()
        query = gdata.youtube.service.YouTubeVideoQuery()
        query.vq = search_terms
        query.orderby = 'relevance'
        query.racy = 'include'
        feed = yt_service.YouTubeQuery(query)
        return self.PrintVideoFeed(feed)